<template>
  <div>
    <!-- 单位查询 -->
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <el-form-item label="名称：">
        <el-input v-model="form.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="备注：">
        <el-input v-model="form.notes" placeholder="备注 "></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="GetUnit">查询</el-button>
        <!-- 添加、编辑、删除、导入、导出 -->
        <el-button type="success" @click="dialogTableVisible = true"
          >新建</el-button
        >
        <el-dialog title="单位管理（新建）" :visible.sync="dialogTableVisible">
          <el-form :model="forms" :rules="rules" ref="forms">
            <el-form-item
              label="单位名称"
              prop="name"
              :label-width="formLabelWidth"
            >
              <el-input v-model="forms.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item
              label="单位备注"
              prop="notes"
              :label-width="formLabelWidth"
            >
              <el-input
                type="textarea"
                v-model="forms.notes"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="AddUnit('forms')"
              >确 定</el-button
            >
          </div>
        </el-dialog>

        <el-button type="info" @click="GetUnitById">编辑</el-button>
        <el-dialog title="单位管理（编辑）" :visible.sync="dialogFormVisible">
          <el-form :model="formUpd" :rules="rules1" ref="formUpd">
            <el-form-item
              label="单位名称"
              prop="name"
              :label-width="formLabelWidth"
            >
              <el-input v-model="formUpd.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item
              label="单位备注"
              prop="notes"
              :label-width="formLabelWidth"
            >
              <el-input
                type="textarea"
                v-model="formUpd.notes"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="UpdateUnit('formUpd')"
              >确 定</el-button
            >
          </div>
        </el-dialog>

        <el-button type="warning" @click="DeleleUnit">删除</el-button>
        <el-button type="danger" @click="GetUnit">导入</el-button>
        <el-button type="danger" @click="GetUnit">导出</el-button>
      </el-form-item>
    </el-form>
    <!-- 单位信息 -->
    <el-table
      ref="multipleTable"
      :data="table"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column prop="Id"> </el-table-column>
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="Name" label="名称"> </el-table-column>
      <el-table-column prop="Notes" label="备注"> </el-table-column>
      <el-table-column prop="CreateDate" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.CreateDate.replace("T", " ").substr(0, 19) }}
        </template>
      </el-table-column>
      <el-table-column prop="CreatorId" label="创建人"> </el-table-column>
      <el-table-column prop="Updator" label="修改人"> </el-table-column>
      <el-table-column prop="UpdateDate" label="修改时间">
        <template slot-scope="scope">
          {{ scope.row.UpdateDate.replace("T", " ").substr(0, 19) }}
        </template></el-table-column
      >
    </el-table>
    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pageIndex"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        name: "",
        notes: "",
      },
      forms: {
        creatorId: sessionStorage.getItem("UserId"),
        updator: sessionStorage.getItem("UserId"),
        name: "",
        notes: "",
        isDel: false,
      },
      formUpd: {
        name: "",
        notes: "",
      },
      rules: {
        name: [
          { required: true, message: "单位名称不能为空！", trigger: "blur" },
        ],
      },
      rules1: {
        name: [
          { required: true, message: "单位名称不能为空！", trigger: "blur" },
        ],
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      table: [], //单位信息
      multipleTable: [], //选中的单位信息
      page: {
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0,
        totalCount: 0,
      },
    };
  },
  methods: {
    //全选、全取消
    handleSelectionChange(val) {
      this.multipleTable = val;
    },
    //每页显示
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.GetUnit();
    },
    //当前页
    handleCurrentChange(val) {
      this.page.pageIndex = val;
      this.GetUnit();
    },
    //分页、显示、查询单位信息
    GetUnit() {
      this.axios
        .get("http://localhost:5255/api/Unit/GetUnit", {
          params: {
            pageIndex: this.page.pageIndex,
            pageSize: this.page.pageSize,
            name: this.form.name,
            notes: this.form.notes,
          },
        })
        .then((res) => {
          this.table = res.data.Data.List;
          this.page.totalCount = res.data.Data.TotalCount;
          this.page.pageCount = res.data.Data.PageCount;
        });
    },
    //添加单位信息
    AddUnit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("http://localhost:5255/api/Unit/AddUnit", this.forms)
            .then((res) => {
              if (res.data) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.dialogTableVisible = false;
                this.GetUnit();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除单位信息
    DeleleUnit() {
      if (this.multipleTable.length == 0) {
        this.$message({
          message: "请选择要删除的信息",
          type: "warning",
        });
        return;
      }
      let id = 0;
      this.multipleTable.forEach((item) => {
        id = item.Id;
      });
      this.$confirm("是否删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete("http://localhost:5255/api/Unit/DeleleUnit?id=" + id)
            .then((res) => {
              if (res.data) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.GetUnit();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //反填单位信息
    GetUnitById() {
      if (this.multipleTable.length == 0) {
        this.$message({
          message: "请选择要编辑的信息",
          type: "warning",
        });
        return;
      }
      this.dialogFormVisible = true;
      let id = 0;
      this.multipleTable.forEach((item) => {
        id = item.Id;
      });
      this.axios
        .get("http://localhost:5255/api/Unit/GetUnitById?id=" + id)
        .then((res) => {
          this.formUpd.name = res.data.Data.Name;
          this.formUpd.notes = res.data.Data.Notes;
        });
    },
    //修改单位信息
    UpdateUnit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .put("http://localhost:5255/api/Unit/UpdateUnit", {
              id: this.multipleTable[0].Id,
              name: this.formUpd.name,
              notes: this.formUpd.notes,
              creatorId: this.multipleTable[0].CreatorId,
              createDate: this.multipleTable[0].CreateDate,
              updator: sessionStorage.getItem("UserId"),
              isDel: this.multipleTable[0].IsDel,
            })
            .then((res) => {
              if (res.data) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.GetUnit();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.GetUnit();
  },
};
</script>